// pages/address/address.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: [],
    jsCode: ''
  },
  wAddress: function () {
    wx.navigateTo({
      url: "../addresses/addresses"
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.address();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   this.address()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  address:function(){
    var that = this;
    wx.login({
      success: function (res) {
        // console.log(res)
        that.setData({ jsCode: res.code })
        wx.request({
          url: getApp().globalData.domain + 'api/receiving_addres/list',
          data: {
            openid: getApp().globalData.openid,
            jsCode: that.data.jsCode
            // token: getApp().globalData.userInfo.token
          },
          header: {
            "Content-Type": "applciation/json"
          },
          method: "GET",
          success: function (res) {
            console.log(res.data);
            that.setData({
              address: res.data,
            })
          },
          fail: function (err) { },
          complete: function () { }
        })
      }
    })
  },
  edit: function (e) {
    var that = this;
    // console.log(e)
    var index = e.currentTarget.dataset.index
    // console.log(index)
    var id = this.data.address[index].id
    // console.log(this.data.address[index])
    wx.navigateTo({
      url: '../addressedit/addressedit?id='+id
    })
  },
  del: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index
    var id = this.data.address[index].id
    wx.request({
      url: getApp().globalData.domain + 'api/receiving_addres/delete?id=' + id,
      data: {
        // id: id,
        // openid: getApp().globalData.openid,
        // token: getApp().globalData.userInfo.token
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "POST",
      success: function (res) {
        that.address();
      },
      fail: function (err) { },
      complete: function () { }
    });
  },
  addSelect:function(e){
    getApp().globalData.addressId = e.currentTarget.dataset.id
    wx.navigateBack({
      delta: 1,
    })
  }
})